/*
 * @Autor: liukesen
 * @Date: 2020-07-31 10:39:05
 * @LastEditors: liukesen
 * @LastEditTime: 2020-08-02 18:19:03
 */
const nav_list = [{
    id: 0,
    e_id: 0,
    text: '飞包首页',
    english_text: 'Home',
    e_path: './english_html/index.html',
    path: './chinese_html/index.html'
  }, {
    id: 1,
    text: '关于我们',
    path: './chinese_html/about-us.html'
  }, {
    id: 2,
    text: '关于公司',
    path: './chinese_html/company.html'
  }, {
    id: 3,
    text: '联系我们',
    path: './chinese_html/contact-us.html'
  }, {
    id: 4,
    e_id: 1,
    text: '用户指南',
    english_text: 'AirBbag user guide',
    e_path: './english_html/user-guide.html',
    path: './chinese_html/user-guide.html'
  }, {
    id: 5,
    e_id: 2,
    text: '旅行者',
    english_text: 'Traveller',
    e_path: './english_html/traveles.html',
    path: './chinese_html/traveles.html'
  }],
  sce4_list = [{
    id: 0,
    text: '飞包愿景',
    e_text: 'Real Duty Free',
    e_p: 'High duty items: 20-45% cheape',
    imgUrl: '../images/sce4_1.jpg',
    e_url: '../images/e_sec4_01.webp'
  }, {
    id: 1,
    text: '飞包使命',
    e_text: 'Huge Savings',
    e_p: 'Making $300 dolloars per trip!',
    imgUrl: '../images/sce4_2.jpg',
    e_url: '../images/e_sec4_02.webp'

  }, {
    id: 2,
    text: '飞包价值观',
    e_text: 'Realtime travelers',
    e_p: 'Travlers and Buyers is just one Flight Away!',
    imgUrl: '../images/sce4_3.jpg',
    e_url: '../images/e_sec4_03.webp'
  }, {
    id: 3,
    text: '飞包理念',
    e_text: 'Lightening Fast Delivery',
    e_p: 'Fastest international Delivery on the planet',
    imgUrl: '../images/sce4_4.jpg',
    e_url: '../images/e_sec4_04.webp'
  }],
  heard_dom = `
    <a class="hd_logo" id="logo_icon"></a>
    <ul class="hd_nav"></ul>
    <div class="hd_extra">
      <ul>
        <li id="chinese" class="change_e" data-fial='1'><i class="change chinese"></i>中文</li>
        <li class="l"></li>
        <li id="english" class="change_e" data-fial='0'><i class="change english"></i>English</li>
      </ul>
    </div>
 `,
  footer_dom = ` <div class="footer">
  <div class="container">
    <div class="footer_link">
      <div class="ft-help">
        <div class="bd-hot">
          飞包客服咨询热线<br/> 132 6927 7243
        </div>
        <div class="bd-hot">
          飞包客服邮箱 <br/>xm.li@godoorway.com
        </div>
      </div>
      <div class="ft-menu"> 
        <div class="wx-code">
          <span>小程序</span>
        </div>
      </div>
    </div>
    <div class="footer_copy">
      <p>Copyright by © 艾歌科技有限公司 2020 版权所有 </p>
    </div>
  </div>
</div>`;
let nav_index = 0, // 头部导航栏index
  change_index = 1; // 语言 0为英语  1为中文

change_index = this.parseQueryString(window.location.href).change;
$(document).ready(() => {
  $('#content').load(`/${change_index==1?'chinese_html':'english_html'}/index.html`, () => {
    $('.sec4-list').html(this.getSecListDom(sce4_list));
  });

  $('#header').html(heard_dom);
  $('#footer').html(footer_dom);
  $('.hd_nav').html(this.getNavListDom(nav_list));
  this.addOnclicFn.call(this);

  $('.change_e').click((e) => {
    const E = e.currentTarget.dataset.fial;
    if (change_index == E) return;
    change_index = E;
    window.location.href = change_index == 1 ? '/pc_index.html?change=1&' : '/e_pc_index.html?change=0&';
  })

  $('#logo_icon').click(() => {
    $('#content').html('').load(`/${change_index==1?'chinese_html':'english_html'}/index.html`, () => {
      $('.sec4-list').html(this.getSecListDom(sce4_list));
      nav_index = 0;
      for (let i = 0; i < $('.nav-link').length; i++) {
        $('.nav-link').eq(i).removeClass('nav-link-hover');
        $('.nav-link').eq(i).addClass('nav-link-show');
        if (i == 0) {
          $('.nav-link').eq(i).removeClass('nav-link-show');
          $('.nav-link').eq(i).addClass('nav-link-hover');
        }
      }
    });
  })
})

/**
 * @description: 获取头部导航栏
 * @param {type} 导航栏数据
 * @return: header的dom
 * @author: liukesen
 */
function getNavListDom(arr = []) {
  let _dom = '';
  for (let i = 0; i < arr.length; i++) {
    const element = arr[i];
    let {
      id,
      path,
      e_path,
      text,
      english_text,
      _src,
      e_id
    } = element;
    _src = change_index == 1 ? path : e_path;
    if (change_index == 0 && english_text == undefined) continue;
    _dom += `<li class="nav_item"><div class="nav-link ${id!=nav_index?'nav-link-show':'nav-link-hover'}" data-src="${_src}" data-id="${change_index==1?id:e_id}">${change_index==0?english_text:text}</div></li>`;
  }
  return _dom;
}

/**
 * @description: 获取照片墙dom
 * @param {type} 照片墙数据
 * @return: 照片墙dom
 * @author: liukesen
 */
function getSecListDom(arr) {
  let _dom = '';
  for (let i = 0; i < arr.length; i++) {
    const element = arr[i];
    let {
      text,
      e_text,
      e_p,
      imgUrl,
      e_url
    } = element,
    dom_text = change_index == 1 ? `<div>${text}</div>` : `<div>
    <p style="font-size: 20px;line-height: 44px;">${e_text}</p>
    <p style="font-size:14px;color:rgb(153, 153, 153)">${e_p}</p>
    </div>`;
    _dom += change_index == 1 ? `<li style="background-image:url(${change_index==1?imgUrl:e_url});">${dom_text}</li>` : `<li><div class="col-image" style="background-image:url(${change_index==1?imgUrl:e_url});"></div>${dom_text}</li>`
  }
  return _dom;
}

function parseQueryString(urlStr) {
  // ?号后面的位置
  var pos = urlStr.indexOf("?") + 1;
  // 参数的字符串
  var paramsStr = urlStr.substring(pos);
  // 声明对象
  var obj = {};
  // 把参数添加到对象中
  var paramsArr = paramsStr.split("&");
  console.log(paramsArr)
  // 数组循环
  paramsArr.forEach(function (element) {
    // element == key0=aaa
    var _arr = element.split("=");
    // 往对象里添加内容
    obj[_arr[0]] = _arr[1];
  });
  // 返回对象
  return obj;
}

function addOnclicFn() {
  $('.nav-link').click((e) => {
    $('#content').removeClass('jinchang-an')
      .addClass('chuchang-an');
    $('footer').fadeOut();
    setTimeout(() => {
      $('#content').load(`${e.currentTarget.dataset.src}`).removeClass('chuchang-an')
        .addClass('jinchang-an');
      $('footer').fadeIn();
    }, 300);
    for (let i = 0; i < $('.nav-link').length; i++) {
      console.log(e.currentTarget.dataset.id)
      $('.nav-link').eq(i).removeClass('nav-link-hover');
      $('.nav-link').eq(i).addClass('nav-link-show');
      if (e.currentTarget.dataset.id == i) {
        $('.nav-link').eq(i).removeClass('nav-link-show');
        $('.nav-link').eq(i).addClass('nav-link-hover');
      }
    }
  })
}